package com.sky.controller.admin;


import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("adminCategoryController")
@RequestMapping("/admin/category")
@Slf4j
@CrossOrigin
public class CategoryController {

    @Autowired
    private CategoryService categoryService;


    @DeleteMapping
    @ApiOperation("根据id删除分类")
    public Result<String> deleteById(Long id){
        log.info("根据id删除分类：{}",id);
        categoryService.deleteById(id);

        return Result.success();
    }

    @GetMapping("/list")
    @ApiOperation("根据类型查询分类")
    public Result<List<Category>> listByType(Integer type){
        log.info("根据类型查询分类:{type}");

        List<Category> list = categoryService.listByType(type);

        return Result.success(list);
    }


    @GetMapping("/page")
    @ApiOperation("分页查询")
    public Result<PageResult> page(CategoryPageQueryDTO categoryPageQueryDTO){
        log.info("分页查询：{}",categoryPageQueryDTO);
        PageResult result=categoryService.page(categoryPageQueryDTO);


        return Result.success(result);
    }


    @PostMapping()
    @ApiOperation("新增分类")

    public Result<String> insert(@RequestBody CategoryDTO categoryDTO){
        log.info("新增分类:{}",categoryDTO);
        categoryService.insert(categoryDTO);
        return Result.success();
    }

    @PostMapping("/status/{status}")
    @ApiOperation("修改分类状态")
    public Result<String> modifyStatus(@PathVariable Integer status,Long id){
        log.info("修改分类状态 id:{},status:{}",id,status);

        categoryService.modifStatus(status,id);

        return Result.success();

    }

    @PutMapping
    @ApiOperation("修改分类信息")
    public Result<String> update(@RequestBody CategoryDTO categoryDTO){
        log.info("修改分类信息：{}",categoryDTO);
        categoryService.update(categoryDTO);

        return Result.success();
    }
}
